package com.itc.dispatch.common;

import com.itc.system.api.RemoteSystemService;
import com.itc.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;


@Component
public class FindAndProcessParent {

    @Autowired
    public   RemoteSystemService remoteSystemService;

    public ArrayList<SysDept> findParentName(SysDept sysDept) {
        ArrayList<SysDept> sysDeptArrayList = new ArrayList<>();
        if (sysDept != null){
            findParentsAddList(sysDept, sysDeptArrayList);

        }else {
            return null;
        }
        return sysDeptArrayList;
    }
    private void findParentsAddList(SysDept sysDept, ArrayList<SysDept> sysDeptArrayList) {
        // 获取当前部门的父级ID
        Long deptId = sysDept.getParentId();

        // 如果父级ID不等于0
        if (deptId != null && deptId.intValue() != Constents.Int_0) {
            // 查找父级部门
            SysDept parentDept = remoteSystemService.getDeptById(deptId);

            // 添加到列表中
            sysDeptArrayList.add(parentDept);

            // 递归查找父级的父级
            findParentsAddList(parentDept, sysDeptArrayList);
        }else {
            //添加部门分割线
            sysDeptArrayList.add(new SysDept("-----------"));
        }
    }
}
